/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package mini_game_fx;

import java.io.File;
import java.net.MalformedURLException;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.PixelReader;
import javafx.scene.image.PixelWriter;
import javafx.scene.image.WritableImage;
import javafx.scene.paint.Color;

/**
 *
 * @author Joshua
 */
public class MiniGameImageView extends ImageView implements Cloneable{
    
    private String path;
    private int width;
    private int height;
    private Color colorKey = null;
    
    private int loadMethod;//which method to use to load image
    
    
    public MiniGameImageView clone(){
        MiniGameImageView temp = null;
        
        switch(this.loadMethod){
            case 1: 
                temp = new MiniGameImageView(path);
                break;
            case 2: 
                temp = new MiniGameImageView(path, width, height);
                break;
            case 3: 
                temp = new MiniGameImageView(path, width, height, true);
                break;
            case 4: 
                temp = new MiniGameImageView(path, width, height, colorKey);
                break;
        }
//        System.out.println("\tMiniGameImageView here in clone");
//        System.out.println("MiniGameImageView here in clone\n"
//                + "\tOriginal Image = " + this.getImage()
//                + "\tNew Image      = " + temp.getImage());
        return temp;
    }
    
    public MiniGameImageView(){
        path = null;
        width = -1;
        height = -1;
    }
    
    public MiniGameImageView(String path){
        super(loadImage(path));
        this.path = path;
        this.loadMethod = 1;
    }
    
    public MiniGameImageView(WritableImage wi){
        super(wi);
//        this.path = path;
//        this.loadMethod = 1;
    }
    
    public MiniGameImageView(String path, int width, int height) {
        super(loadImage(path, width, height));
        this.width = width;
        this.height = height;
        this.path = path;
        this.loadMethod = 2;
    }

    public MiniGameImageView(String path, int width, int height, boolean background) {
        super(loadImageAsBackground(path, width, height));
    this.width = width;
        this.height = height;
        this.path = path;
        this.loadMethod = 3;
    }

    public MiniGameImageView(String path, int width, int height, Color colorKey) {
        super(loadImageWithColorKey(path, width, height, colorKey));
        this.path = path;
        this.colorKey = colorKey;
        this.width = width;
        this.height = height;
        this.path = path;
        this.loadMethod = 4;
    }

    public static Image loadImage(String fileName) {
        File file = new File(fileName);
        if (file.exists()) {
            fileName = file.getAbsolutePath();
        }
        Image img = null;
        try {
//                System.out.println(file.toURI().toURL().toExternalForm());
            img = new Image(file.toURI().toURL().toExternalForm());

        } catch (Exception e) {
            System.out.println("image could not be loaded ");
            e.printStackTrace();
        }
        return img;

    }


        /**
         * loads image with custom width and height
         * uses default non-image-smoothing algorithm
         * @param fileName
         * @param width
         * @param height
         * @return 
         */
        public static Image loadImage(String fileName, int width, int height){
            
            //TODO must implement same funtionality as other loadImage function
            System.out.println("\tloadImage(String fileName, int width, int height)");
            File file = new File(fileName);
            if(file.exists())
                fileName = file.getAbsolutePath();
            Image img = null;
            try{
                img= new Image(file.toURI().toURL().toExternalForm(), width, height, false, false, false);
            }catch(MalformedURLException e){
                e.printStackTrace();
            }
//            Image img = new Image(fileName, width, height, false, false, false);
            return img;
            
        }
        
        /**
         * load image with custom width and height
         * with intent of being used as background
         * uses default non-image-smoothing-algorithm
         * @param fileName
         * @param width
         * @param height
         * @return 
         */
        public static Image loadImageAsBackground(String fileName, int width, int height){
            
            //TODO must implement same funtionality as other loadImage function
//            System.out.println("\tloadImageAsBackground(String fileName, int width, int height)");
            //TODO this function might not be necessary 
            Image img = new Image(fileName, width, height, false, false, true);
            return img;
        }

	/**
	 * Loads an image using the fileName as the full path,
	 * returning the constructed and completely loaded Image. Note
	 * that all pixels with the colorKey value will be made
	 * transparent by setting their alpha values to 0.
	 * 
	 * @param fileName full path and name of the location of
	 * the image file to be loaded.
	 * 
	 * @return the loaded Image, with all data fully loaded and
	 * with colorKey pixels transparent.
	 */
	public static Image loadImageWithColorKey(String fileName, int width, int height, Color colorKey)
	{
//            System.out.println("\tloadImageWithColorKey(String fileName, int width, int height, Color colorKey) test?");
            //TODO must implement with same Functionality as original method
//            System.out.println("mingameFx - incomplete loadImagewithColorKey");
//            System.out.println("file path; " + fileName);
            Image img = null;
            ImageView iv = new ImageView();
            File file = new File(fileName); 
            try{
                if(width == 0 && height == 0){
                    img= new Image(file.toURI().toURL().toExternalForm());
                }else{
//                    img= new Image(getClass().getResource(fileName).toExternalForm(), width, height, false, false);
                    img= new Image(file.toURI().toURL().toExternalForm(), width, height, false, false);
                }
            }catch(MalformedURLException e){
                e.printStackTrace();
                System.exit(0);
            }
           
            WritableImage wi = new WritableImage((int)img.getWidth(), (int)img.getHeight());
            PixelWriter px = wi.getPixelWriter();
            iv.setImage(img);
            
            PixelReader pr = iv.getImage().getPixelReader();
            
            for(int x = 0; x < img.getWidth(); x ++){
                for(int y = 0; y < img.getHeight(); y ++){
                    Color c = pr.getColor(x, y);
                    Color temp;
                    if(c.getBlue() == colorKey.getBlue() && c.getRed() == colorKey.getRed() && c.getGreen() == colorKey.getGreen()){
                        temp = new Color(c.getRed(), c.getBlue(), c.getGreen(), 0);
//                        System.out.println("match at pixle (" + x +", " + y + ")" );
                    }else{
                        temp = new Color(c.getRed(), c.getBlue(), c.getGreen(), 1);
                    }
                    px.setColor(x, y, temp);
                        
                }
            }
            return wi;
        }
    
}
